import {
	HTTP
} from '../../common/js/http.js'


import util from '../../common/js/util.js'

export class registerModel extends HTTP {
	Login(phone, pwd,isChoose) { // 登录接口
		const userCID = util.getStorage('userCID')
		this.request({
				url: 'api/Users/Login',
				data: {
					userName: phone,
					userPwd: pwd,
					userCID: userCID
				}
			})
			.then(data => {
				util.setStorage('isChoose',isChoose)
				this._storeUser(data)
				this._audit(data)
			})
	}

	SendSMSVerify(phone) { // 短信 
		this.request({
				url: 'api/Users/SendSMSVerify',
				data: {
					phone: phone
				}
			})
			.then(res => {
				util.tost('短信发送成功')
			})
	}

	Enroll({ userPhone, userPwd, captcha }) {  // 注册
		return this.request({
			url: 'api/Users/Register',
			data: {
				userPhone: userPhone,
				userPwd: userPwd,
				captcha: captcha
			}
		})
	}
	
	Forget ({ userPhone, userPwd, captcha }) {  // 忘记密码
		return this.request({
			url: 'api/Users/ForgetPassword',
			data: {
				userPhone: userPhone,
				userPwd: userPwd,
				captcha: captcha
			}
		})
	}
	
	getCode(phone, callback) {
		if (!util.verifyPhone(phone)) {
			return
		}
		let time = 60,
			codetxt, codeBtn;
		this.SendSMSVerify(phone)
		var countdown = setInterval(() => {
			if (time <= 1) {
				clearInterval(countdown)
				codetxt = '重新获取';
				codeBtn = false;
			} else {
				time--
				codetxt = time + 's后获取';
				codeBtn = true
			}
			return callback(codetxt, codeBtn)
		}, 1000)
	}





	_storeUser(data) {
		uni.setStorage({
			key: 'user',
			data: data
		})
	}

	_audit(data) {
		if (data.stand == -1) {
			// 去填写业主审核信息
		} else {
			if (data.stand < 4) {
				uni.showModal({
					content: `系统检测出你的账号在员工端已被注册，,
					无法登陆成功请联系客服人员`,
					confirmText: "立即联系",
					success: (res) => {
						if (res.confirm) {
							uni.makePhoneCall({
								phoneNumber: '18209208297'
							})
						}
					}
				})
				return
			}
			uni.switchTab({
				url: '/pages/home/index/index'
			});
		}
	}

}
